using System;
using System.Collections.Generic;
using System.Linq;

namespace RecommendedProduct
{
	public class ProductGameTypeConfig
	{
		private int _countButton = 4;

		private Random random = new Random();

		public List<Product> GetProduct(TypeGame typeGame)
		{
			List<Product> list = new List<Product>();
			switch (typeGame)
			{
			case TypeGame.normal:
			case TypeGame.TDM:
			case TypeGame.Offline:
			case TypeGame.Pixelmon:
				AddRange(list, GetProductsByCategorySimple("Weapons", "Melee"));
				AddRange(list, GetProductsByCategorySimple("Rides", "Cars"));
				AddRange(list, GetProductsByCategorySimple("Weapons", "Guns"));
				AddRange(list, GetProductsByCategorySimple("Armor", "Body"));
				AddRange(list, GetAutomaticOrShotgun());
				AddRange(list, GetBootsOrHats());
				AddRange(list, GetProductsByCategorySimple("Weapons", "Special"));
				AddRange(list, GetProductsByCategory(new SelectHandlerRandom(), "Weapons", "Gear"));
				break;
			case TypeGame.Zombe:
			case TypeGame.CopsAndRobbers:
			case TypeGame.SkyBlock:
				AddRange(list, GetProductsByCategorySimple("Weapons", "Melee"));
				AddRange(list, GetProductsByCategorySimple("Armor", "Body"));
				AddRange(list, GetProductsByCategorySimple("Weapons", "Guns"));
				AddRange(list, GetProductsByCategorySimple("Armor", "Boots"));
				AddRange(list, GetAutomaticOrShotgun());
				AddRange(list, GetProductsByCategorySimple("Armor", "Hats"));
				AddRange(list, GetProductsByCategorySimple("Weapons", "Special"));
				AddRange(list, GetProductsByCategory(new SelectHandlerRandom(), "Weapons", "Gear"));
				break;
			case TypeGame.RocketLeague:
				AddRange(list, GetRaceCars(_countButton - 1));
				list.Add(MarketController.Instance.GetProduct("Boost"));
				break;
			case TypeGame.race:
				AddRange(list, GetRaceCars(_countButton));
				break;
			case TypeGame.tanks:
			{
				SelectHandlerNext selectHandlerNext2 = new SelectHandlerNext();
				selectHandlerNext2.Count = _countButton;
				AddRange(list, GetProductsByCategory(selectHandlerNext2, "Rides", "Tanks"));
				break;
			}
			case TypeGame.WalkingRobots:
			case TypeGame.TitanfallV2:
			{
				SelectHandlerNext selectHandlerNext = new SelectHandlerNext();
				selectHandlerNext.Count = _countButton;
				AddRange(list, GetProductsByCategory(selectHandlerNext, "Rides", "Titans"));
				break;
			}
			}
			return list;
		}

		public void AddRange(List<Product> results, List<Product> products)
		{
			results.AddRange(products);
			ProductStaticManager.GetInstance().AddProductToIgnore(products);
		}

		private List<Product> GetRaceCars(int count)
		{
			SelectHandlerNext selectHandlerNext = new SelectHandlerNext();
			string category = "Rides";
			string subCategory = "Cars";
			selectHandlerNext.Count = count;
			List<Product> listSortProducts = MarketController.Instance.GetListSortProducts(category, subCategory);
			listSortProducts.RemoveAll((Product x) => ((ProductCar)x).notForRace);
			List<Product> products = FilterProducts(listSortProducts, ProductStaticManager.GetInstance().IgnoreProducts);
			products = selectHandlerNext.GetProduct(products);
			if (products.Count >= selectHandlerNext.Count)
			{
				return products;
			}
			ProductStaticManager.GetInstance().ClearProductByCategory(category, subCategory);
			products = FilterProducts(listSortProducts, ProductStaticManager.GetInstance().IgnoreProducts);
			SelectHandlerRandom selectHandlerRandom = new SelectHandlerRandom();
			selectHandlerRandom.Count = count;
			return selectHandlerRandom.GetProduct(products);
		}

		public List<Product> GetAutomaticOrShotgun()
		{
			List<Product> list = new List<Product>();
			List<ShotType> list2 = new List<ShotType>();
			SelectHandlerNext selectHandlerNext = new SelectHandlerNext();
			list2.Add(ShotType.Automatic);
			list2.Add(ShotType.Spread);
			list2 = list2.OrderBy((ShotType _) => random.Next()).ToList();
			string category = "Weapons";
			string subCategory = "Rifles";
			List<Product> collection = FilterProducts(MarketController.Instance.GetListSortProducts(category, subCategory), ProductStaticManager.GetInstance().IgnoreProducts);
			ShotType tmpShotTyp2;
			foreach (ShotType item in list2)
			{
				tmpShotTyp2 = item;
				list = new List<Product>(collection);
				list.RemoveAll((Product x) => ((ProductWeapon)x).shotType != tmpShotTyp2);
				list = selectHandlerNext.GetProduct(list);
				if (list.Count > 0)
				{
					return list;
				}
			}
			ProductStaticManager.GetInstance().ClearProductByCategory(category, subCategory);
			collection = FilterProducts(MarketController.Instance.GetListSortProducts(category, subCategory), ProductStaticManager.GetInstance().IgnoreProducts);
			ShotType tmpShotTyp;
			foreach (ShotType item2 in list2)
			{
				tmpShotTyp = item2;
				list = new List<Product>(collection);
				list.RemoveAll((Product x) => ((ProductWeapon)x).shotType != tmpShotTyp);
				list = selectHandlerNext.GetProduct(list);
				if (list.Count > 0)
				{
					return list;
				}
			}
			return GetProductsByCategory(new SelectHandlerRandom(), "Weapons", "Gear");
		}

		public List<Product> GetBootsOrHats()
		{
			List<Product> list = new List<Product>();
			List<string> list2 = new List<string>();
			list2.Add("Boots");
			list2.Add("Hats");
			list2 = list2.OrderBy((string _) => random.Next()).ToList();
			foreach (string item in list2)
			{
				list = GetProductsByCategorySimple("Armor", item);
				if (list.Count > 0)
				{
					return list;
				}
			}
			if (list.Count > 0)
			{
				return list;
			}
			return GetProductsByCategory(new SelectHandlerRandom(), "Weapons", "Gear");
		}

		private List<Product> GetProductsByCategorySimple(string category, string subCategory)
		{
			List<Product> productsByCategory = GetProductsByCategory(new SelectHandlerNext(), category, subCategory);
			if (productsByCategory.Count > 0)
			{
				return productsByCategory;
			}
			return GetProductsByCategory(new SelectHandlerRandom(), "Weapons", "Gear");
		}

		private List<Product> GetProductsByCategory(SelectHandlerAbstract handler, string category, string subCategory)
		{
			List<Product> products = FilterProducts(MarketController.Instance.GetListSortProducts(category, subCategory), ProductStaticManager.GetInstance().IgnoreProducts);
			products = handler.GetProduct(products);
			if (products.Count >= handler.Count)
			{
				return products;
			}
			ProductStaticManager.GetInstance().ClearProductByCategory(category, subCategory);
			products = FilterProducts(MarketController.Instance.GetListSortProducts(category, subCategory), ProductStaticManager.GetInstance().IgnoreProducts);
			return handler.GetProduct(products);
		}

		private List<Product> FilterProducts(List<Product> products, List<Product> ignoreProduct = null)
		{
			List<Product> list = new List<Product>(products);
			if (ignoreProduct != null)
			{
				list.RemoveAll((Product x) => ignoreProduct.Contains(x));
			}
			return list;
		}
	}
}
